<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="1000px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="区域名称" prop="areaId">
          <ApiSelect
            v-model="queryParams.areaId"
            :api="selectState.areaList.api"
            :params="{ pageNum: 1, pageSize: 999999, cityName: queryParams.cityName }"
            :resultField="selectState.areaList.resultField"
            :valueField="selectState.areaList.valueField"
            :labelField="selectState.areaList.labelField"
            :immediate="true"
            placeholder="请选择区域"
          />
        </el-form-item>
        <el-form-item label="线路名称" prop="routeId">
          <ApiSelect
            class="form-input"
            v-model="queryParams.routeId"
            :api="selectState.route.api"
            :params="{ areaId: queryParams.areaId }"
            :valueField="selectState.route.valueField"
            :labelField="selectState.route.labelField"
            :immediate="false"
            placeholder="请选择线路"
          />
        </el-form-item>
        <el-form-item label="点位名称" prop="positionId">
          <ApiSelect
            class="form-input"
            v-model="queryParams.positionId"
            :api="selectState.point.api"
            :params="{ routeId: queryParams.routeId }"
            :valueField="selectState.point.valueField"
            :labelField="selectState.point.labelField"
            :immediate="false"
            placeholder="请选择点位"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="machineName">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.machineName"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="machineId">
          <el-input
            style="width: 200px"
            v-model.trim="queryParams.machineId"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleQuery">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="multipleTableRef" v-loading="loading" size="default" index :data="dataList">
        <el-table-column
          label="点位名称"
          show-overflow-tooltip
          align="center"
          prop="positionName"
        />
        <el-table-column label="设备名称" show-overflow-tooltip align="center" prop="machineName" />
        <el-table-column label="设备编号" show-overflow-tooltip align="center" prop="machineId" />
        <el-table-column label="已售份数" align="center" prop="soldCopies" />
        <el-table-column label="已售份数占比" align="center" prop="soldCopiesRate">
          <template #default="{ row }"> {{ row.soldCopiesRate || 0 }}% </template>
        </el-table-column>
        <el-table-column label="已售总金额(元)" align="center" prop="totalSalesAmount" />
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <div class="total-box">
        <span>已售份数总计：{{ totalValue.soldCopies }}</span>
        <span>已售总金额总计：{{ totalValue.totalSalesAmount }}</span>
        <span>已售份数占比：{{ totalValue.soldCopiesRate }}%</span>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { getPackItemSalesListApi } from '@/api/statistic'
import { useOrganApiConfig } from '@/hooks/service'
const queryRef = ref(null)
const props = defineProps({
  validStartTime: {
    type: String,
    default: ''
  }
})

const emits = defineEmits(['close'])

const selectState = useOrganApiConfig()
const total = ref(0)
const dataList = ref([])
const visible = ref(false)
const title = ref('')

const totalValue = ref({})

const queryParams = reactive({
  startDate: '',
  endDate: '',
  cityName: '',
  areaId: '',
  routeId: '',
  positionId: '',
  machineName: '',
  machineId: '',
  pageNum: 1,
  pageSize: 10
})
const loading = ref(true)

const multipleTableRef = ref(null)

/** 查询用户列表 */
function getList() {
  loading.value = true
  getPackItemSalesListApi(queryParams)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
      totalValue.value = res?.data?.data || {}
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

function onClose() {
  queryRef.value.resetFields()
  queryParams.pageNum = 1
}

function init(row, params) {
  visible.value = true
  queryParams.packId = row.packId
  queryParams.cityName = params.cityName
  queryParams.startDate = params.startDate
  queryParams.endDate = params.endDate
  title.value = row.packName
  getList()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.total-box {
  display: flex;
  justify-content: flex-end;
  margin-right: 20px;
  margin-top: 40px;
  span {
    margin-right: 30px;
  }
}
</style>
